package com.linkstec.wechat;

import java.util.HashMap;
import java.util.concurrent.CountDownLatch;

import com.linkstec.util.HttpAsyncClientUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WeComSendService {

    // 基础支持: 获取access_token接口 /token
    // https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwc929cbbf84a98436&corpsecret=yWw-qJyx2907SBn5T1yaDUH0dVHOmxhQpAD6T7dldWw
    public static void main(String[] args) {

        String access_token = "2yIEeI_Vn489WlyjOWcd12jZMZIQNyOh7BQkm9kg5Al1ObjgyahlnycPWQU0VpKEn2K8CtO-nVefoFa1gHRPLv3OSKNSgieMUpOUTzNufRo4JNmnGKt2rRl9RfSyAGe0POmODcw0uAHuh-uPIun1_QEVY8aS7je_UQP8HVQqotGUmqTDcaUBgQWRTMEZ1W3RW0GPgW3cqt4gWFXIbIZbfw";
        String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + access_token;

        HashMap<String, Object> postMap = new HashMap<String, Object>();
        postMap.put("touser", "LiuYongLu");
        postMap.put("toparty", "gEjWlN3Pd1N8am7vRSSGSygAgJc7vzYpfQSNTm71-_I");
        postMap.put("totag", "http://www.baidu.com");
        postMap.put("msgtype", "text");
        postMap.put("agentid", 1000002);
        HashMap<String, Object> data = new HashMap<String, Object>();
        data.put("content",
                "你的快递已到，请携带工卡前往邮件中心领取。\\n出发前可查看<a href=\\\"http://work.weixin.qq.com\\\">邮件中心视频实况</a>，聪明避开排队。");
        postMap.put("text", data);
        postMap.put("safe", 0);
        postMap.put("enable_id_trans", 0);
        postMap.put("enable_duplicate_check", 0);
        postMap.put("duplicate_check_interval", 1800);
        int postSize = 1;
        CountDownLatch countDownLatchPost = new CountDownLatch(postSize);
        HttpAsyncClientUtil.getClient();
        HttpAsyncClientUtil.startHttpAsyncClient();
        HttpAsyncClientUtil.post(url, postMap, countDownLatchPost);
        try {
            countDownLatchPost.await();
        } catch (InterruptedException e) {
            log.error("", e);
        } finally {
        }

        // 构造请求
        String geturl = "http://www.linkstec.com";

        long start = System.currentTimeMillis();
        int size = 1000;
        CountDownLatch countDownLatch = new CountDownLatch(size);
        HttpAsyncClientUtil.startHttpAsyncClient();

        for (int i = 0; i < size; i++) {
            HttpAsyncClientUtil.get(geturl, countDownLatch);
        }
        try {
            countDownLatch.await();

            log.info(size + "次整体cost is:" + (System.currentTimeMillis() - start));
            log.info("结束");
            HttpAsyncClientUtil.closeHttpAsyncClient();
        } catch (InterruptedException e) {
        }

    }

}